package com.bong.ring.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bong.ring.R;

/**
 * Created by PanXuesen on 2018/2/1.
 */

public class MenuItemView {
    String title;
    String[] items;
    int[] icons;
    Context context = null;
    private View.OnClickListener clickListener;

    public MenuItemView(String title,
                        String[] items,
                        int[] icons,
                        Context context) {
        this.title = title;
        this.items = items;
        this.icons = icons;
        this.context = context;
    }

    public View creat() {
        LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_menu, null, false);
        TextView textView = view.findViewById(R.id.textView);
        TextView button = view.findViewById(R.id.button);
        View imageView = view.findViewById(R.id.imageView);
        View line1 = view.findViewById(R.id.line1);
        View line2 = view.findViewById(R.id.line2);
        View layout = view.findViewById(R.id.layout);
        layout.setId(0);
        layout.setOnClickListener(clickListener);
        View tempView;
        textView.setText(title);
        button.setText(items[0]);
        imageView.setBackgroundResource(icons[0]);
        for (int i = 1; i < items.length; i++) {
            View temp = LayoutInflater.from(context).inflate(R.layout.layout_menu_item, null, false);
            button = temp.findViewById(R.id.button);
            imageView = temp.findViewById(R.id.imageView);
            layout = temp.findViewById(R.id.layout);
            button.setText(items[i]);
            imageView.setBackgroundResource(icons[i]);
            layout.setId(i);
            layout.setOnClickListener(clickListener);
            view.addView(temp);
            tempView = line2;
            line2 = new View(context);
            line2.setLayoutParams(tempView.getLayoutParams());
            line2.setBackground(tempView.getBackground());
            view.addView(line2);
        }
        line2.setVisibility(View.GONE);
        line2 = new View(context);
        line2.setLayoutParams(line1.getLayoutParams());
        line2.setBackground(line1.getBackground());
        view.addView(line2);
        return view;
    }

    public MenuItemView setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
        return this;
    }
}
